package com.jwdf.uda.ui.activity;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.LayQuizQuickBinding;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.question.index.QuizQuickHotFragment;
import com.jwdf.uda.ui.fragment.question.index.QuizQuickNewFragment;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.view.tabstrip.AdvancedPagerSlidingTabStrip;
/**
 * Created by fatsfan on 2016/10/17.
 */

public class QuizQuickActivity extends AbsActivity implements View.OnClickListener {

    private final int SIZE = 2;
    LayQuizQuickBinding mBinding;
    int posSel = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.lay_quiz_quick);
        LinearLayout.LayoutParams flParams = (LinearLayout.LayoutParams) mBinding.rlQuiz.getLayoutParams();
        flParams.height += DisplayUtils.getStatusBarHeight(this);
        mBinding.rlQuiz.setLayoutParams(flParams);
        mBinding.tvHeadTitle.setPadding(0, DisplayUtils.getStatusBarHeight(this), 0, 0);
        mBinding.flHeadBack.setPadding(DisplayUtils.dipToPix(this,15), DisplayUtils.getStatusBarHeight(this), DisplayUtils.dipToPix(this,15), 0);
        mBinding.flHeadBack.setOnClickListener(this);
        initVP();
    }

    @Override
    protected boolean fullScreenMode() {
        return true;
    }

    private void initVP() {
        final ViewPager vp = mBinding.vp;

        vp.setOffscreenPageLimit(SIZE);
        Adapter adapter = new Adapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        final AdvancedPagerSlidingTabStrip vTabs = mBinding.vTabs;
        vTabs.setViewPager(vp);
        vTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if(posSel != position) {
                    final TextView txt = vTabs.getTxt(position);

                    final int size = DisplayUtils.px2sp(QuizQuickActivity.this, txt.getTextSize());
                    final int unitSP = TypedValue.COMPLEX_UNIT_SP;
                    vTabs.getTxt(posSel).setTextSize(unitSP, size);
                    txt.setTextSize(unitSP, size + 1);

                    posSel = position;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setCurrentItem(0);

        final TextView txt = vTabs.getTxt(posSel);

        final int size = DisplayUtils.px2sp(QuizQuickActivity.this, txt.getTextSize());
        final int unitSP = TypedValue.COMPLEX_UNIT_SP;
        txt.setTextSize(unitSP, size + 1);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.fl_head_back:
                onBackPressed();
                break;
        }
    }

    class Adapter extends FragmentPagerAdapter {
        String[] strs = new String[SIZE];
        BaseFragment[] frags = new BaseFragment[SIZE];

        public Adapter(FragmentManager fm) {
            super(fm);
            frags[0] = new QuizQuickNewFragment();
            frags[1] = new QuizQuickHotFragment();

            for(int i = 0; i < SIZE; i++)
                strs[i] = frags[i].getTitle();
        }

        @Override
        public Fragment getItem(int position) {
            return frags[position];
        }

        @Override
        public int getCount() {
            return SIZE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return strs[position];
        }
    }

}
